﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dami;
public partial class Clients_Control_Index : System.Web.UI.UserControl
{
    private Random random = new Random();
    //phan nen tin tuc tren trang chu.a lam no o day
    private String strNew = "<div class='box_new'><a href='$link'><img src='$src' width='100' alt='' class='$class' />$title</a><br><br>"
        + "$text<br/></div>";
    private String strImage = "<img id='img_customer' src='" + Constant.ROOT_PATH + "$src' />";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack) {
            LoadDataRight();
            LoadYKien();
            String strPage = "";
            try
            {
                strPage = Request.Params["Pro"].ToString().Trim();
            }
            catch
            {
                strPage = "Default";
            }
            lblContentTitle.Text = Constant.ContentTitle(strPage);
        }
    }

    private void LoadYKien() {
        WebData db = new WebData();
        List<DataLibrary.tbl_YKienKhachHang> list = db.tbl_YKienKhachHangs.GetListCache("ykienkh",Constant.time);
        int l = list.Count;
        
        int index1 = Convert.ToInt32(this.random.Next(l).ToString());
        index1 = index1 == l ? l - 1 : index1;
        if (index1 < 0 ) {
            index1 = 0;
        }
        
        DataLibrary.tbl_YKienKhachHang item = list[index1];
        String strHinh =  item.Hinh;
        lblImage.Text = strImage.Replace("$src",strHinh);
        lblNoiDung.Text = item.NoiDung;
        lblGiamDoc.Text = item.HoVaTen;
        lblCty.Text = item.CongTy;

    }
    private int Index(String str, String find)
    {
        int vt = -1;
        vt = str.IndexOf(find);
        return vt;
    }
    private void LoadDataRight(){

        //Hien thi mau tin tren trang chu
        int mautin = -1;
        int mota = 230;
        if (!string.IsNullOrEmpty(Constant.ViewIndex))
        {
            try
            {
                mautin = Convert.ToInt32(Constant.ViewIndex);
                mota = Convert.ToInt32(Constant.Mota);
            }
            catch { }
        }
        mautin = mautin == -1 ? 4 : mautin;
        using (DamiWebDataContext db = new DamiWebDataContext())
        {
            var query = from p in db.tbl_News_Langs
                        where p.tbl_New.idNew == p.idNew
                        && p.idLang == 1 && p.tbl_New.isTindacbiet == false && p.tbl_New.isAuto==true
                        && p.tbl_New.isActive==1 
                        orderby p.tbl_New.orderId descending, p.idNew descending
                        select new
                        {
                            p.titleNew,
                            p.idNew,
                            p.tbl_New.imageNew,
                            p.tbl_New.addedDate,
                            p.bodyNew,
                            p.introNew,
                            p.tbl_New.isAuto
                        };

            List<News> list = new List<News>();
            int i = 0;
            
            foreach (var c in query)
            {
                News news = new News();
                news.idNew = c.idNew;
                String strFind = "";// c.introNew;
                int begin, end;
                if (c.isAuto == true)
                {
                    strFind = c.introNew;
                    begin = Index(strFind, "src=\"http://");
                    end = Index(strFind, ".jpg");
                    if (begin != -1 && end > begin + 3)
                    {
                        strFind = strFind.Substring(begin + 5, end - begin - 1);
                    }
                    else {
                        strFind = "";
                    }
                }
                
                String strRece = c.introNew;
                begin = Index(strRece, "<a");
                end = Index(strRece, "</a>");
                if (end > begin)
                {
                    strRece = strRece.Substring(begin, end + 1);
                }
                else {
                    strRece = "";
                }
                if (!String.IsNullOrEmpty(c.imageNew)) {
                    strFind = c.imageNew;
                }
                news.imageNew = strFind;
                if (String.IsNullOrEmpty(c.introNew))
                {
                    news.introNew = c.bodyNew;
                }
                else
                {
                    news.introNew = c.introNew;
                    if (!String.IsNullOrEmpty(strRece)) {
                        news.introNew = c.introNew.Replace(strRece, "");
                    }
                    news.introNew = Dami.clsFileUpLoad.DeHtml(news.introNew).Trim();
                }
                news.titleNew = c.titleNew;
                list.Add(news);
                i++;
                if (i == mautin)
                    break;
            }

            //tin tuc cong ty
            var cty = from p in db.tbl_News_Langs
                        where p.tbl_New.idNew == p.idNew
                        && p.idLang == 1 && p.tbl_New.isTindacbiet == false && p.tbl_New.isAuto == false
                        && p.tbl_New.isActive == 1 
                      orderby p.tbl_New.orderId descending,p.idNew descending
                        select new
                        {
                            p.titleNew,
                            p.idNew,
                            p.tbl_New.imageNew,
                            p.tbl_New.addedDate,
                            p.bodyNew,
                            p.introNew
                        };
            int dem = 0;
            String strNews = "";
            i = 0;
            int l = list.Count;
            foreach (var c in cty) { 
               
                
                String strlink = Constant.ROOT_PATH + String.Format("{0}/{1}/{2}", Constant.StrChiTietTinTuc,c.idNew, c.titleNew.ToString().
                                Replace(" ", "-").Replace(".", "").Replace("/", "_").Replace(":", "").Replace("\"", ""));

                String strPath =String.IsNullOrEmpty(Constant.Dami) == true ? Constant.ROOT_PATH : Constant.Dami;
                //Hien thi mau tin

                String sImage = "";
                sImage = c.imageNew == null ? "" :  c.imageNew.ToString().Replace("~/", "");
                String strSrc = sImage;
                String strClass = sImage=="" ? "hideImg" : "";
                String strTitle =c.titleNew;
                if (strTitle.Length >Constant.lenghtTitle) {
                    strTitle = strTitle.Substring(0, Constant.lenghtTitle) + " ...";
                }
                String strText =c.introNew.ToString().Trim().Replace("<p>","").Replace("</p>","");
                if (strText.Length > mota){
                    strText = strText.Substring(0, mota) + " ...";
                }
                if (!clsFileUpLoad.CheckPath(strSrc)) {
                    strSrc = "upload/news/no_image.jpg";
                }

                strSrc = strPath + strSrc;
                strNews = strNews + strNew.Replace("$link", strlink).Replace("$src", strSrc).
                    Replace("$class", strClass).Replace("$title", strTitle).Replace("$text", strText);

                //tin tuc cap nhat tu dong
                strlink = Constant.ROOT_PATH + String.Format("{0}/{1}/{2}", Constant.StrChiTietTinTuc,list[i].idNew,
                    Constant.ChangeString(list[i].titleNew.ToString()));
                 strSrc = list[i].imageNew;
                 strClass = "";// "hideImg" + c.imageNew.ToString();
                 strTitle = list[i].titleNew;
                 strText = list[i].introNew.Replace("<p>","").Replace("</p>","");
                 if (!clsFileUpLoad.CheckPath(strSrc))
                 {
                     strSrc =  "upload/news/no_image.jpg";
                 }
                 if (strSrc.IndexOf("http") == -1) {
                     strSrc = strPath + strSrc;
                 }
                 if (strText.Length > mota)
                 {
                     strText = strText.Substring(0, mota) + " ...";
                 }
                 if (strTitle.Length > Constant.lenghtTitle)
                 {
                     strTitle = strTitle.Substring(0, Constant.lenghtTitle) + " ...";
                 }
                 strNews = strNews + strNew.Replace("$link", strlink).Replace("$src", strSrc).
                     Replace("$class", strClass).Replace("$title", strTitle).Replace("$text", strText);
                dem++;
                i++;
                if (dem == l)
                   break;
            }
            lblNews.Text = strNews.Trim().Replace("<p>","").Replace("</p>","");
            //LoadDataLeft(arr);
        }

        
    }

}